<template>
  <div class="fulltext-container">
    <el-upload
      ref="uploader"
      class="upload"
      :action="uploadUrl"
      :multiple="multiple"
      :on-success="uploadSuccess"
      :show-file-list="false"
      :headers="headers"
    />
    <quill-editor
      ref="quillEditor"
      :value="value"
      :options="editorOption"
      min-height="500px"
      @input="updateVal($event)"
    />
  </div>
</template>

<script>

export default {
  name: 'JSFullText',
  components: { },
  model: {
    prop: 'value',
    event: 'input'
  },
  props: {
    headers: {
      type: Object,
      default: () => {}
    },
    action: {
      type: String,
      required: true
    },
    multiple: {
      type: Boolean,
      default: false
    },
    sortByFileName: {
      type: Boolean,
      default: false
    },
    placeholder: {
      type: String,
      default: '请输入'
    },
    value: {
      type: String,
      default: ''
    },
    responseFn: {
      type: Function,
      default: null
    }
  },
  data() {
    return {
      uploadImageCount: 0,
      uploadImageTotalCount: 0,
      editorOption: { // 富文本编辑器配置
        placeholder: this.placeholder,
        modules: {
          toolbar: {
            container: [
              ['bold', 'italic', 'underline', 'strike'], // 加粗，斜体，下划线，删除线
              // ['blockquote', 'code-block'], // 引用，代码块
              [{ 'header': 1 }, { 'header': 2 }], // 标题，键值对的形式；1、2表示字体大小
              [{ 'list': 'ordered' }, { 'list': 'bullet' }], // 列表
              [{ 'script': 'sub' }, { 'script': 'super' }], // 上下标
              [{ 'indent': '-1' }, { 'indent': '+1' }], // 缩进
              [{ 'direction': 'rtl' }], // 文本方向
              // [{ 'size': ['small', false, 'large', 'huge'] }], // 字体大小
              // [{ 'header': [1, 2, 3, 4, 5, 6, false] }], // 几级标题
              [{ 'color': [] }, { 'background': [] }], // 字体颜色，字体背景颜色
              // [{ 'font': [] }], // 字体
              [{ 'align': [] }], // 对齐方式
              ['clean'], // 清除字体样式
              ['image'] // 上传图片、上传视频
            ],
            handlers: {
              'image': function(value) {
                if (value) {
                  document.querySelector('.upload input').click()
                } else {
                  this.quill.format('image', false)
                }
              }
            }
          }
        },
        theme: 'snow'
      }
    }
  },
  computed: {

  },
  methods: {
    uploadSuccess(e, file, fileList) {
      // console.log(fileList)
      // 不需要根据文件名排序
      if (!this.sortByFileName) {
        if (this.responseFn) {
          const url = this.responseFn(file.response, file)
          const quill = this.$refs.quillEditor.quill
          const length = quill.getSelection().index // 获取光标所在位置
          quill.insertEmbed(length, 'image', url) // 插入图片
          quill.setSelection(length + 1)
        }
      } else {
        // 需要根据文件名排序
        if (this.uploadImageTotalCount === 0) {
          this.uploadImageTotalCount = fileList.length
        }
        this.sortByFiles(fileList)
        this.uploadImageCount = this.uploadImageCount + 1
        if (this.uploadImageTotalCount === this.uploadImageCount) {
          console.debug(fileList)
          if (this.responseFn) {
            for (const fileInfo of fileList) {
              const url = this.responseFn(fileInfo.response, file)
              const quill = this.$refs.quillEditor.quill
              const length = quill.getSelection().index // 获取光标所在位置
              quill.insertEmbed(length, 'image', url) // 插入图片
              quill.setSelection(length + 1)
            }
          }
          this.clearUpload()
        }
      }
    },
    updateVal(val) {
      this.$emit('input', val)
    },
    clearUpload() {
      this.uploadImageTotalCount = 0
      this.uploadImageCount = 0
      this.$refs.uploader.clearFiles()
    },
    sortByFiles(files) {
      files.sort(function(a, b) { return a.name.localeCompare(b.name) })
    }
  }
}
</script>

<style scoped>

.fulltext-container /deep/  .quill-editor {
  background:#fff;
  height: 100%;

}
.fulltext-container /deep/  .ql-container {
  min-height: 180px;
  height: 70%;
  max-height: 1000px;
   overflow-y: scroll;
  /* resize: vertical; */
}

.fulltext-container /deep/  .el-upload-list--text {
  display: none;
}

</style>
